
 /*
 * cpe301assignment1.asm
 *
 *  Created: 2/15/2014 12:30:00 PM
 *   Author: Jose Camacho
 */ 
 ;Used X and Y pointer as mentioned in the instructions
 ;Stored total sum in R3:R4 ( Used to check if under 16 bits)
 ;Stored the sum of all numbers divisible by 3 in R20:R21
 ;Stored the sum of all numbers divisible by 5 in R23:R24

  .org 0
  
  LDI R26, HIGH(RAMEND) ;Initialize the stack here
  OUT SPH, R26
  LDI R26, LOW(RAMEND)
  OUT SPL, R26
  LDI R18, 20        ;Counter
  LDI R17, 100        ;The value that I will store (>30 but <250)
  
  LoadStack:
   PUSH R17          ;Push the value I selected into the stack 
   INC R17           ;Increase to get the next value
   DEC R18           ;Counter gets decreased here 
   BRNE LoadStack    ;While the counter is not equal to zero keep loading values into the stack.

  LDI ZH, HIGH(RAMEND) ;This will set Z to point to Stack
  LDI ZL, LOW(RAMEND)
  LDI YH, HIGH(RAMEND) ;This will set Y to point to Stack
  LDI YL, LOW(RAMEND)
  LDI XH, HIGH(RAMEND) ;This will set X to point to Stack
  LDI XL, LOW(RAMEND)
  LDI R20, 0           ;R20 and R21 will store the divisible by three sums
  LDI R21, 0
  LDI R22, 0           ;R22 is used for getting the quotient
  MOV R10, R22
  LDI R23, 0           ;R23 and R24 will store the divisible by five sums
  LDI R24, 0		   
  LDI R18, 20		   ;Counter to add up the 20 numbers
  MOV R15, R18
  MOV R14, R18
  LDI R16, 255         ;Used for checking total bits under 16
  LDI R25, 3
  
  ; --- TotalSum is used to make sure that our total sum of the 20 numbers is under 16 bits.
  ; In my case, the Total Sum was 0x0316 in Hex, as seen in the processor view, which is under.

  TotalSum:        
  LD R1, Y           ;Load Y into R15
  ADD R3, R1         ;R3 in the Processor view has the lower bit for the Total Sum.
  ADC R4,R2          ;R4 in the Processor view has the higher bit for the Total Sum.          
  DEC YL             ;This will decrease the pointer to the next place in memory
  DEC R18
  BRNE TotalSum		 ;While the counter is not equal to zero keep going
  CP R4,R16
  BRLO TotalUnderSixteenBits	 ;This will make sure our total sum is under 16 bits, then move to check divisibility if true. 
  
  TotalUnderSixteenBits:
  LD R5, X			 ;Load X into R5
  MOV R19, R5        ;Move value from R5 to R19. Used to add numbers if divisible by 3
  DEC XL             ;This will decrease the pointer to the next place in memory.
  DEC R15
  BREQ GoToFive      ;Go to check if divisible by is possible
  CLR R22            ;CLR R22, will be used again 
  rJMP CheckDivByThree 

  CheckDivByThree:   ;This is the division by three model, if it is divisible by three we then go to DivByThree
  INC R22			 ;Increment Quotient	
  SUB R5, R25		 ;Subtract 3 from R5 until we get 0 or less
  BRCC CheckDivbyThree

  ADD R5, R25        ;If R5 = 0 at this point, it means that our number is divisible by 3
  BREQ DivByThree
  DEC R22			 ;Decrement to get our quotient
  BRNE TotalUnderSixteenBits    ;Branch to get next number
  
  DivByThree:
  ADD R20, R19		;R19 contains the number divisible by 3, stores that number into lower bit in R20
  ADC R21, R23      ;R21 contains the higher bit
  rJMP TotalUnderSixteenBits

  GoToFive:
  CLR R19
  LDI R19, 5		;Divide by 5 register
  LD R6, Z			;Load Z into R6
  MOV R11, R6		;Move R6 into R11. This number will be used to add to sum of divisible by five.
  DEC ZL
  DEC R14			;Decrement Counter
  BREQ DoneWithOne
  CLR R22
  rJMP CheckDivByFive

  CheckDivByFive:   ;This is the division by five model, if it is divisible by five we go to DivByFive
  INC R10			;Increment Quotient
  SUB R6, R19
  BRCC CheckDivByFive

  ADD R6, R19		;If R6 = 0 at this point, it means that our number is divisible by 5
  BREQ DivByFive	
  DEC R10			;Decrement to get our quotient
  BRNE GoToFive		;Branch to get next number 

  DivByFive:
  ADD R23, R11		;R23 contains the number divisible by 5, stores that number into lower bit in R23
  ADC R24, R12		;R24 contains the higher bit
  rJMP GoToFive


DoneWithOne:
JMP DoneWithOne
  

